package pikasial.graphique;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import pikasial.images.PikImage;
import pikasial.images.tags.*;

public class TagBox extends JPanel {

	private static final long serialVersionUID = -1385961669514430674L;

	private ImageIcon persoIcon;
	private ImageIcon personIcon;
	private ImageIcon placeIcon;
	private ImageIcon eventIcon;

	private JList tags;
	private JTextField textField;
	private ButtonGroup group;
	private JRadioButton perso;
	private JRadioButton person;
	private JRadioButton place;
	private JRadioButton event;

	private JButton ok;
	private JButton add;
	private JButton suppr;

	protected PikImage image;

	private boolean readyToClose;

	public TagBox(PikImage pik) {
		super();
		setLayout(new BorderLayout());
		image = pik;
		initComponents();
		setPreferredSize(new Dimension(650,200));
	}


	private void initComponents() {
		JPanel gauche = new JPanel();
		JPanel centre = new JPanel(new BorderLayout());
		JPanel bas = new JPanel();

		JScrollPane scrollPane = new JScrollPane(/*JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER*/);
		persoIcon = new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.PERSO_TAG_ICON_PATH));
		personIcon = new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.PEOPLE_TAG_ICON_PATH));
		eventIcon = new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.EVENT_TAG_ICON_PATH));
		placeIcon = new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.PLACE_TAG_ICON_PATH));

		tags = new JList();
		image.getTags();
		tags.setModel(image.getTags());
		tags.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tags.setCellRenderer(new TagsListRenderer());
		tags.setPreferredSize(new Dimension(180,120));
		tags.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				if (!tags.isSelectionEmpty()) {
					suppr.setEnabled(true);
					Tag t = (Tag) tags.getSelectedValue();
					textField.setText(t.getDescription());
					if (t instanceof TagPerso)
						perso.setSelected(true);
					else if (t instanceof TagEvenement)
						event.setSelected(true);
					else if (t instanceof TagPersonne)
						person.setSelected(true);
					else
						place.setSelected(true);
				} else {
					suppr.setEnabled(false);
					textField.setText("");
				}
			}
		});

		//scrollPane.setViewportView(tags);
		gauche.add(tags);
		scrollPane.setViewportView(gauche);

		ok = new JButton("Ok",new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.OK_ICON_PATH)));
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				close();
			}
		});
		bas.add(ok);

		group = new ButtonGroup();
		perso = new JRadioButton("Perso",true);
		perso.setIcon(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.PERSO_TAG_GRAY_ICON_PATH)));
		perso.setRolloverIcon(persoIcon);
		perso.setSelectedIcon(persoIcon);
		person = new JRadioButton("Personne(s)");
		person.setIcon(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.PEOPLE_TAG_GRAY_ICON_PATH)));
		person.setRolloverIcon(personIcon);
		person.setSelectedIcon(personIcon);
		place = new JRadioButton("Lieu");
		place.setIcon(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.PLACE_TAG_GRAY_ICON_PATH)));
		place.setRolloverIcon(placeIcon);
		place.setSelectedIcon(placeIcon);
		event = new JRadioButton("Evenement");
		event.setIcon(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.EVENT_TAG_GRAY_ICON_PATH)));
		event.setRolloverIcon(eventIcon);
		event.setSelectedIcon(eventIcon);
		group.add(perso);
		group.add(person);
		group.add(place);
		group.add(event);

		JPanel buttonGroupPane = new JPanel();
		JPanel pane = new JPanel();
		buttonGroupPane.add(perso);
		buttonGroupPane.add(person);
		buttonGroupPane.add(place);
		buttonGroupPane.add(event);
		textField = new JTextField(25);
		pane = new JPanel();
		pane.add(textField);
		centre.add(buttonGroupPane,BorderLayout.NORTH);
		centre.add(pane,BorderLayout.CENTER);
		pane = new JPanel();
		add = new JButton(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.ADD_ICON_PATH)));
		add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				if (!textField.getText().trim().equals("")) {
					String text = textField.getText();
					Tag t = null;
					if (perso.isSelected())
						t = new TagPerso(text);
					else if (person.isSelected())
						t = new TagPersonne(text);
					else if (event.isSelected())
						t = new TagEvenement(text);
					else
						t = new TagLieu(text);
					image.getTags().addTag(t);
				}
			}
		});
		suppr = new JButton(new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.REMOVE_ICON_PATH)));
		suppr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				Object t = tags.getSelectedValue();
				if (t != null) {
					image.getTags().removeTag((Tag) t);
				}
			}
		});
		if (image.getTags().getSize() == 0) {
			suppr.setEnabled(false);
		}
		pane.add(add);
		pane.add(suppr);
		centre.add(pane,BorderLayout.EAST);

		add(scrollPane,BorderLayout.WEST);
		add(centre,BorderLayout.CENTER);
		add(bas,BorderLayout.SOUTH);
	}

	public synchronized void close() { readyToClose = true; notifyAll(); }
	public synchronized boolean isReadyToClose() { return readyToClose; }

	@SuppressWarnings("serial")
	class TagsListRenderer extends javax.swing.DefaultListCellRenderer {
		static final int MAX_TEXT_LENGTH = 20;

		public TagsListRenderer() { super(); }

		public java.awt.Component getListCellRendererComponent(final JList list,
		        final Object value,
		        final int index,
		        final boolean isSelected,
		        final boolean cellHasFocus) {
			JLabel l = (JLabel) super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
			l.setPreferredSize(new Dimension(120,30));
			String s = ((Tag) value).getDescription();
			if (s.length() >= MAX_TEXT_LENGTH) {
				s = s.substring(0, MAX_TEXT_LENGTH).concat("...");
			}
			l.setText(s);
			l.setIcon(((Tag) value).getLowResIcon());
			l.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			return l;
		}
	}
}
